package com.siyuan.blog.web;

import com.siyuan.blog.common.Constants;
import com.siyuan.blog.common.vo.JSONResponse;
import com.siyuan.blog.common.vo.Menu;
import com.siyuan.blog.domain.system.Resources;
import com.siyuan.blog.domain.system.User;
import com.siyuan.blog.service.system.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author tianxuan
 * @version V1.0
 * @Date 2017/10/16 0016 上午 10:44
 * @Description: 登录/退出控制器
 */
@Controller
@RequestMapping
public class    LoginController {
    @Autowired
    private LoginService loginService;


    @RequestMapping("/login")
    public String loginUI(){
        return "login";
    }

    /**
     * @return
     */
    @RequestMapping("/login/verify")
    @ResponseBody
    public JSONResponse verify(String userName, String password, String captcha, HttpServletRequest request) throws SQLException {

        int code = JSONResponse.CODE_SUCCESS;
        String msg = "登陆成功";
        HttpSession session = request.getSession();
        String sessionCode = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        Date date = (Date)session.getAttribute(Constants.KAPTCHA_SESSION_DATE);
        if(null == sessionCode || null == captcha || !sessionCode.equals  (captcha) || ((System.currentTimeMillis() - date.getTime()) /1000/ 60 > 15) ) {
            code = JSONResponse.CODE_FAIL;
            msg = "验证码错误或失效";
        }else {
            User user = loginService.login(userName, password);
            if(null != user) {
                session.setAttribute(Constants.SK_USER, user);    //登陆成功则将用户信息存入session
                List<Menu> menus = loginService.menus(user.getRoleId());
                session.setAttribute(Constants.SK_MENUS, menus); //将主页菜单存进session
                List<Resources> topMenus = loginService.topMenus(user.getRoleId());
                session.setAttribute(Constants.SK_TOP_MENUS, topMenus); //将主页顶部快速导航菜单存进session
                Map<String, Resources> resourcesMap = loginService.myResources(user.getRoleId());
                session.setAttribute(Constants.SK_ALL_RESOURCES, resourcesMap); //将该用户所有权限存进session
                Map<String, List<String>> pageSubResCode = loginService.myPageResources(user.getRoleId());
                session.setAttribute(Constants.SK_PAGE_SUB_RES_CODE, pageSubResCode); //将该用户每个页面拥有的操作权限code存进session
            }else {
                code = JSONResponse.CODE_FAIL;
                msg = "用户名或密码错误";
            }
        }
        session.removeAttribute(Constants.KAPTCHA_SESSION_KEY);
        session.removeAttribute(Constants.KAPTCHA_SESSION_DATE);
        return new JSONResponse(code, msg);
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return "redirect:/login";
    }
}
